package com.zijiebutiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zijiebutiao.mapper.TMemberMapper;
import com.zijiebutiao.mapper.TOrderMapper;
import com.zijiebutiao.mapper.TSetmealMapper;
import com.zijiebutiao.pojos.TMember;
import com.zijiebutiao.pojos.TOrder;
import com.zijiebutiao.pojos.TSetmeal;
import com.zijiebutiao.service.BusinessService;
import com.zijiebutiao.service.TSetmealService;
import com.zijiebutiao.utils.ThreadPoolUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CountDownLatch;

/**
* @author ZhangQingTian
* @description 针对表【t_member】的数据库操作Service实现
* @createDate 2023-03-03 18:47:53
*/
@Transactional(rollbackFor = Exception.class)
@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private TMemberMapper tMemberMapper;

    @Autowired
    private TOrderMapper tOrderMapper;
    @Autowired
    private TSetmealMapper tSetmealMapper;


    @Override
    public Map<String, Object> getData() throws InterruptedException {
        HashMap<String, Object> map = new HashMap<>(16);

        CountDownLatch cdl=new CountDownLatch(10);

        ThreadPoolUtils.threadPoolExecutor().submit(new Runnable(){
            @Override
            public void run() {
                try {
                    map.put("totalMember",tMemberMapper.memberCount());
                }finally {
                    cdl.countDown();
                }

            }
        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                map.put("todayNewMember",tMemberMapper.todayNewMember());
            }finally {
                cdl.countDown();
            }

        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                map.put("thisWeekNewMember",tMemberMapper.weekNewMember());
            }finally {
                cdl.countDown();
            }

        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                map.put("thisMonthNewMember",tMemberMapper.monthNewMember());
            }finally {
                cdl.countDown();
            }

        });





        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                map.put("todayOrderNumber",tOrderMapper.todayNewOrder());
            }finally {
                cdl.countDown();
            }

        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                map.put("todayVisitsNumber",tOrderMapper.todayNewOrderConfirm());
            }finally {
                cdl.countDown();
            }

        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                map.put("thisWeekOrderNumber",tOrderMapper.weekNewOrder());
            }finally {
                cdl.countDown();
            }

        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                map.put("thisWeekVisitsNumber",tOrderMapper.weekNewOrderConfirm());
            }finally {
                cdl.countDown();
            }

        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                map.put("thisMonthOrderNumber",tOrderMapper.monthNewOrder());
            }finally {
                cdl.countDown();
            }

        });
        ThreadPoolUtils.threadPoolExecutor().submit(()->{
            try {
                map.put("thisMonthVisitsNumber",tOrderMapper.monthNewOrderConfirm());
            }finally {
                cdl.countDown();
            }

        });

        cdl.await();
        map.put("reportDate",new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return map;
    }

    @Override
    public List getDataList() {
        List list=new ArrayList<>();
        List<TSetmeal> setmeals = tSetmealMapper.selectList(null);

        setmeals.stream().forEach(s->{
            HashMap<String, String> map = new HashMap<>();
            map.put("name",s.getName());

            LambdaQueryWrapper<TOrder> wq=Wrappers.lambdaQuery();
            wq.eq(TOrder::getSetmealId,s.getId());
            Long count = tOrderMapper.selectCount(wq);
            Long counts = tOrderMapper.selectCount(null);

            NumberFormat numberFormat = NumberFormat.getNumberInstance();
            numberFormat.setMaximumFractionDigits(1);
            String Proportion = numberFormat.format((float) count/(float) counts * 100)+"%";

            map.put("setmeal_count",count.toString());
            map.put("proportion",Proportion);
            map.put("remark",s.getRemark());

            list.add(map);
        });
        return list;
    }
}




